package render

import (
	db "gsc-datasets/internal/db/dao"
	datasetEntity "gsc-datasets/internal/rest/render/entity/dataset"
	"gsc-datasets/pkg/util"

	"github.com/go-chi/render"
)

type RenderEntity interface {
	datasetEntity.ListCategoriesAsTreeRow | *datasetEntity.ListCategoriesAsTreeRow |
		datasetEntity.CreateCategoryResponse | *datasetEntity.CreateCategoryResponse |
		datasetEntity.UpdateCategoryResponse | *datasetEntity.UpdateCategoryResponse |
		datasetEntity.DeleteCategoryResponse | *datasetEntity.DeleteCategoryResponse
}

func StringToRender(s string) render.Renderer {
	// Pre-processing before a response is marshalled and sent across the wire
	return &AppBaseResponse{HTTPStatusCode: 200, StatusText: "ok", Data: s}
}

func EntityToRender[T db.DbEntity | db.DbEntityPtr | RenderEntity](i T) render.Renderer {
	// Pre-processing before a response is marshalled and sent across the wire
	return &AppBaseResponse{HTTPStatusCode: 200, StatusText: "ok", Data: i}
}

func EntityToRenderEx[T db.DbEntity | db.DbEntityPtr | RenderEntity](i T, conv func(T) T) render.Renderer {
	// Pre-processing before a response is marshalled and sent across the wire
	return &AppBaseResponse{HTTPStatusCode: 200, StatusText: "ok", Data: conv(i)}
}

func EntityListToRender[T db.DbEntity | db.DbEntityPtr | RenderEntity](i []T) render.Renderer {
	// Pre-processing before a response is marshalled and sent across the wire
	return &AppBaseResponse{HTTPStatusCode: 200, StatusText: "ok", Data: i}
}

func EntityPagedListToRender[T db.DbEntity | db.DbEntityPtr | RenderEntity](total int64, rows []T) render.Renderer {
	// Pre-processing before a response is marshalled and sent across the wire
	// make json output {rows:null => rows: []}
	if len(rows) == 0 {
		rows = make([]T, 0)
	}
	return &AppBaseResponse{HTTPStatusCode: 200, StatusText: "ok", Data: &ListData[T]{ Total: total, Rows: rows}}
}

func EntityListToRenderEx[T db.DbEntity | db.DbEntityPtr | RenderEntity](i []T, conv func(T) T) render.Renderer {
	// Pre-processing before a response is marshalled and sent across the wire
	return &AppBaseResponse{HTTPStatusCode: 200, StatusText: "ok", Data: util.Map(i, conv)}
}

func EntityListToRenderEx2[U db.DbEntity | db.DbEntityPtr, V RenderEntity](i []U, conv func(U) V) render.Renderer {
	// Pre-processing before a response is marshalled and sent across the wire
	return &AppBaseResponse{HTTPStatusCode: 200, StatusText: "ok", Data: util.Map(i, conv)}
}
